//////////////////////////////////////////////////////////////////////////////////////////////////////
// 项目名   : Lock
// 文件名   : BigOoject.h
// 作者     : Aiye
// 日期     : 2025/9/19 06:00
// 邮箱     : 2878548868@qq.com
// 摘要     : 
//////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef LOCK_BIGOOJECT_H
#define LOCK_BIGOOJECT_H
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
#include <mutex>
#include <ostream>
#include <shared_mutex>

/**
 * @brief 模拟一个复杂的结构体类型
 */
struct ComplexData
{
public:
    ComplexData() = default;

    ComplexData(int n_id, const std::string& str_name, const std::vector<int>& vec_value, double d_score)
        : _nId(n_id), _strName(str_name), _vecValue(vec_value), _dScore(d_score)
    {
    }

    ComplexData(ComplexData&& other) noexcept
        : _nId(other._nId), _strName(std::move(other._strName)), _vecValue(std::move(other._vecValue)),
          _dScore(other._dScore)
    {
    }

    ComplexData(const ComplexData& other) = default;

    ComplexData& operator=(const ComplexData& other)
    {
        if (this == &other)
            return *this;
        _nId = other._nId;
        _strName = other._strName;
        _vecValue = other._vecValue;
        _dScore = other._dScore;
        return *this;
    }

    ComplexData& operator=(ComplexData&& other) noexcept
    {
        if (this == &other)
            return *this;
        _nId = other._nId;
        _strName = std::move(other._strName);
        _vecValue = std::move(other._vecValue);
        _dScore = other._dScore;
        return *this;
    }

    ~ComplexData() = default;

public:
    [[nodiscard]] int n_id() const
    {
        return _nId;
    }

    [[nodiscard]] std::string str_name() const
    {
        return _strName;
    }

    [[nodiscard]] std::vector<int> vec_value() const
    {
        return _vecValue;
    }

    [[nodiscard]] double d_score() const
    {
        return _dScore;
    }

    friend std::ostream& operator<<(std::ostream& os, const ComplexData& obj)
    {
        os << "ID:" << obj._nId << ", "
            << "Name:" << obj._strName << ", "
            << "Score:" << obj._dScore << ", "
            << "Values:[";

        for (size_t i = 0; i < obj._vecValue.size(); ++i)
        {
            os << obj._vecValue[i];
            if (i < obj._vecValue.size() - 1)
            {
                os << ", ";
            }
        }
        os << "]";

        return os;
    }

private:
    int _nId{};
    std::string _strName{};
    std::vector<int> _vecValue{};
    double _dScore{};
};

class BigObject
{
public:
    BigObject() = default;

    explicit BigObject(ComplexData&& data) noexcept : _data(std::move(data))
    {
    }

    BigObject(const BigObject& other) = default;

    BigObject(BigObject&& other) noexcept : _data(std::move(other._data))
    {
    }

    ~BigObject() = default;

    friend std::ostream& operator<<(std::ostream& os, const BigObject& obj)
    {
        return os << "_data: " << obj._data;
    }

    friend void swap(BigObject& lhs, BigObject& rhs) noexcept
    {
        if (&lhs == &rhs)
        {
            return;
        }

        std::lock(lhs._mutex , rhs._mutex); // 同时获取两把锁，避免死锁
        std::lock_guard<std::shared_mutex> lock1(lhs._mutex, std::adopt_lock); // 使用std::adopt_lock表明锁已经被获取
        std::lock_guard<std::shared_mutex> lock2(rhs._mutex, std::adopt_lock);

        std::swap(lhs._data, rhs._data);
    }

private:
    ComplexData _data{}; // 成员变量
    mutable std::shared_mutex _mutex{}; // 读写锁
};


#endif //LOCK_BIGOOJECT_H
